package com.neusoft.elm.dao.impl;

import com.neusoft.elm.dao.UserDao;
import com.neusoft.elm.po.User;
import com.neusoft.elm.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDaoImpl implements UserDao {
    private Connection conn = null;
    private PreparedStatement prep = null;
    private ResultSet rs = null;

    @Override
    public int saveUser(User user) throws Exception {
        try {
            conn = DBUtil.getConnection();
            String sql = "insert into user values(?,?,?,?,?,1)";
            prep = conn.prepareStatement(sql);
            //设置参数 userId  password  userName   userSex  userImg delTag
            prep.setString(1,user.getUserId());
            prep.setString(2,user.getPassword());
            prep.setString(3,user.getUserName());
            prep.setInt(4,user.getUserSex());
            prep.setString(5,user.getUserImg());
            int n = prep.executeUpdate();
            return n;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
             DBUtil.close(prep);
        }
    }

    @Override
    public User getUserByIdByPass(String userId, String password) throws Exception {
        try {
            conn = DBUtil.getConnection();
            String sql = "select * from user where userId=? and password=?";
            prep = conn.prepareStatement(sql);
            //设置参数
            prep.setString(1,userId);
            prep.setString(2,password);
            rs = prep.executeQuery();
            User user = null;
            if(rs.next()){
                 user = new User();
                 user.setUserId(rs.getString("userId"));
                 user.setPassword(rs.getString("password"));
                 user.setUserName(rs.getString("userName"));
                 user.setUserSex(rs.getInt("userSex"));
                 user.setUserImg(rs.getString("userImg"));
                 user.setDelTag(rs.getInt("delTag"));
            }
            return user;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            DBUtil.close(rs,prep);
        }
    }

    @Override
    public User getUserById(String userId) throws Exception {
        try {
            conn = DBUtil.getConnection();
            String sql = "select * from user where userId=?";
            prep =conn.prepareStatement(sql);
            //设置
            prep.setString(1,userId);
            rs = prep.executeQuery();
            User user = null;
            if(rs.next()){
                user = new User();
                user.setUserId(rs.getString("userId"));
                user.setPassword(rs.getString("password"));
                user.setUserName(rs.getString("userName"));
                user.setUserSex(rs.getInt("userSex"));
                user.setUserImg(rs.getString("userImg"));
                user.setDelTag(rs.getInt("delTag"));
            }
            return user;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            DBUtil.close(rs,prep);
        }
    }
}
